import os

class Easy_gitee(object):
    def __init__(self, file_size_limit : float = 50.0, unit : str = 'MB', safe_num : float = 2, all_size_limit : float = 500):
        cmd_init = 'git pull'
        os.system(cmd_init)
        print('Pull code is ok')
        if unit == 'MB':
            self.unit_change = 1024 ** 2
        elif unit == 'KB':
            self.unit_change = 1024
        elif unit == 'GB':
            self.unit_change = 1024 ** 3
        else:
            raise ValueError("没有这种计算类型，请输入:'KB' 'MB' 或 'GB'")
        self.uint_name = unit
        self.size_limit = file_size_limit - safe_num
        self.all_size_limit = all_size_limit - safe_num
        self.current_path = None

    def path_print(self):
        # 获取当前工作目录路径
        current_path = os.getcwd()
        print('当前检测的文件夹为:' + current_path)
        self.current_path = current_path

    def get_file_names(self):
        directory = self.current_path
        file_names = []
        for root, dirs, files in os.walk(directory):
            for file in files:
                file_names.append(os.path.join(root, file))
        return file_names

    def file_size_check(self, file_list : list):
        big_file_path_list = []
        big_file_size_list = []
        all_size = 0
        for file_name_i in file_list:
            if '/.git/' in file_name_i:
                pass
            else:
                size = os.path.getsize(file_name_i)
                size = size / self.unit_change
                all_size += size
                if size > self.size_limit:
                    big_file_path_list.append(file_name_i)
                    big_file_size_list.append(size)
        if all_size > self.all_size_limit:
            print("警告! 仓库最大允许 " + str(self.all_size_limit) + self.uint_name + "! 但是你总共发布了 " + str(all_size) + self.uint_name + "!")
            return False
        if len(big_file_path_list) > 0:
            print("警告! 有文件大小 > " + str(self.size_limit) + self.uint_name)
            for i in range(len(big_file_path_list)):
                print("file: " + big_file_path_list[i] + " 太大了! 文件尺寸为：" + str(big_file_size_list[i]) + self.uint_name + "!")
            return False
        else:
            return True
        
    def push_gitee(self):
        self.path_print()
        files = self.get_file_names()
        ok = self.file_size_check(files)
        if ok == True:
            commit_info = input('输入 commit 信息:')
            cmd_add = 'git add .'
            os.system(cmd_add)
            cmd_commit = "git commit -m \"" + commit_info + "\""
            os.system(cmd_commit)
            cmd_push = 'git push'
            os.system(cmd_push)
            print("all thing is ok!")
        else:
            print("有错误发生!")